<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class SiteController extends Controller
{
	public function index()
	{
      /*  echo "12";exit;

	    echo phpinfo();exit;

		$user = new UserModel();
		//$result = $user->findAll();
		
       $lal = $user->where(['user_name'=>'abc123']);
		
		print_r($lal);exit;
		
		return $this->render('index',
			[
				"test"=>"Hello World!"
			]
		);*/

       $log = Logger::getInstance('lize');

   /*     $log = new Logger('lize');

        new StreamHandler('runtime/test.log');*/

        $log->pushHandler(new StreamHandler('runtime/test.log', Logger::DEBUG));

        $log->addWarning("dsdsdsdsd");
// You can now use your logger
      $log->notice('Adding a new user', array('username' => 'Seldaek'));


	}

	public function mongotest()
    {
        $m = new MongoClient();
        $db = $m->test;
        $collection = $db->runoob; // 选择集合
        $document = array(
            "title" => "MongoDB",
            "description" => "database",
            "likes" => 120,
            "url" => "http://www.runoob.com/mongodb/",
            "by"=>"菜鸟教程"
        );
        $insert = $collection->insert($document);
        var_dump($insert);
    }

	public function test()
    {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);
        $redis->watch("test");
        $test = $redis->get("test");
        $val = $test - 1;
        $redis -> multi();
        $redis->set("test",$val);
        $result = $redis->exec();
        var_dump($result);
    }

    public function bingfa()
    {
        $test_user = new TestUserModel();
        $test_goods = new TestGoodsModel();
        $test_buy_good = new TestBuyGoodModel();


        $model = new Model();

        $return_type = false;
        $model->beginTransaction();

        try
        {
            $user_result = $test_user->findRow();

            $result = $test_goods->findRow();

            if(empty($result))
            {
                throw new Exception("ddd");
            }else
            {
                $money = $user_result['money'];

                if($money - $result['goods_price'] >= 0)
                {

                    $goods_id = $result['id'];
                    $goods_num = $result['goods_num'];
                    $update_num = $goods_num - 1;
                    $version = $result['version'];

                    if($goods_num > 0)
                    {
                        $new_version = $version + 1;
                        $where['version'] = $version;

                        $update_good['goods_num'] = $update_num;
                        $update_good['version'] = $new_version;
                        $update =  $test_goods->updateById($goods_id,$update_good,$where);

                        if($update >= 1)
                        {
                            $update_user['money'] = $money - $result['goods_price'];
                            $user_update =  $test_user->updateById($user_result['id'],$update_user);
                            if($user_update <= 0 )
                            {
                                throw new Exception("ddd");
                            }else
                            {
                                $return_type = true;
                            }


                        }else
                        {
                            throw new Exception("ddd");
                        }

                    }else
                    {
                        throw new Exception("ddd");
                    }

                }else
                {
                    throw new Exception("ddd");
                }


            }

        }catch (Exception $e)
        {
            $model->rollBack();
            echo "回滚";
        }

        if($return_type === true)
        {
            $model->commit();
            $insert_array['good_id'] = $goods_id;
            $insert_array['last_num'] = $update_num;
            $insert_array['input_time'] = date("Y-m-d H:i:s",time());
            $insert_array['user_id'] = $user_result['id'];
            $insert = $test_buy_good->Insert($insert_array);
        }

    }

    public function redis()
    {
        $redis = new Redis();
        $redis->connect('127.0.0.1', 6379);

        $redis->watch("test");
        $test = $redis->get("test");

        if(!empty($test))
        {
            $count = intval($test);

            if($count > 0 )
            {
                $val = $count - 1;
                $redis -> multi();
                $redis->set("test",$val);
                $result = $redis->exec();

                if(!empty($result))
                {
                    $test_goods = new TestGoodsModel();
                    $test_buy_good = new TestBuyGoodModel();

                    $goods_id = 4;
                    $update_good['goods_num'] = $val;
                    $update =  $test_goods->updateById($goods_id,$update_good);

                    if($update)
                    {
                        $insert_array['good_id'] = $goods_id;

                        $insert_array['input_time'] = date("Y-m-d H:i:s",time());
                        $insert_array['user_id'] = 2;
                        $insert = $test_buy_good->Insert($insert_array);
                    }
                }
            }else
            {
                echo "number is zero!";
            }
        }else
        {
            echo "number is zero11!";
        }
    }
}
